package com.project.psc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.dto.BiddToPscDto;
import com.project.psc.bean.InviteBean;
import com.project.psc.bean.InviteSupplierBean;
import com.project.psc.mapper.IInviteMapper;
import com.project.psc.mapper.IInviteSupplierMapper;
import com.project.psc.service.IInviteService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class InviteServiceImpl implements IInviteService {
    @Autowired
    private IInviteMapper inviteMapper;
    @Autowired
    private IInviteSupplierMapper inviteSupplierMapper;

    @Override
    public IPage<InviteSupplierBean> cutByItem(Integer pageNO, Integer status, String title, LocalDate startDate, LocalDate endDate,Long supplierId) {
        return inviteMapper.cutByItem(new Page(pageNO, 6), status, title, startDate, endDate,supplierId);
    }

//    public IPage<InviteBean> cutByItem(Integer pageNO, String title, LocalDate startDate, LocalDate endDate) {
//        QueryWrapper<InviteBean> qw = new QueryWrapper<>();
//        qw.orderByDesc("i_date");
//        if (title != null && title.length() != 0) {
//            qw.eq("i_title", title);
//        }
//        if (endDate != null) {
//            qw.le("i_date", endDate);
//        }
//        if (startDate != null) {
//            qw.ge("i_date", startDate);
//        }
//        return inviteMapper.selectPage(new Page<>(pageNO, 6), qw);
//    }


    @Override
    public InviteBean findById(Long id) {
        InviteSupplierBean inviteSupplierBean = inviteSupplierMapper.selectById(id);
        //修改阅读状态
        inviteSupplierBean.setReadStatus(1);

        inviteSupplierMapper.updateById(inviteSupplierBean);

        return inviteMapper.selectById(inviteSupplierBean.getInviteId());
    }


    @Override
    public void update(Long id, Integer status) {
        InviteBean inviteBean = inviteMapper.selectById(id);
        inviteBean.setStatus(1);
        inviteMapper.updateById(inviteBean);
    }
    @Override
   @RabbitListener(queues = "BiddSender")
    public void add(String json) {
        System.err.println(json);
        BiddToPscDto pscDto = JSONObject.parseObject(json, BiddToPscDto.class);
        //判断采购邀请编号是否重复
        QueryWrapper<InviteBean> qw=new QueryWrapper<>();
        qw.eq("i_inviteId",pscDto.getInviteId());
        boolean exists = inviteMapper.exists(qw);
        if (!exists){

            InviteBean invite=new InviteBean();
            invite.setInviteId(pscDto.getInviteId());
            invite.setTitle(pscDto.getBiddTitle());
            invite.setDateTime(pscDto.getDateTime());
            invite.setInfo(pscDto.getInfo());
            inviteMapper.insert(invite);

            long[] refArray = pscDto.getSupplierRefArray();
            for (int i = 0; i < refArray.length; i++) {
                InviteSupplierBean inviteSupplierBean=new InviteSupplierBean();
                inviteSupplierBean.setSupplierId(refArray[i]);
                inviteSupplierBean.setInviteId(invite.getId());
                inviteSupplierMapper.insert(inviteSupplierBean);

            }

        }




    }

}
